
//THIS CODE FILE PRODUCES FIG 1, FIG 2, FIG A.5

global main "[YOUR PATH HERE]"
global output "${main}/output"

//switch to rerun the build stacked data do file
local switch_rerun_stacking = 0

//enrollment type switches
local switch_all = 0
local switch_nponly  = 0
local switch_fponly  = 0
	local switch_ftonly  = 0
	local switch_ptonly  = 0
	local switch_pubonly = 1
	local switch_prionly = 0
	local switch_menonly = 0
	local switch_womenonly = 0
	local switch_msu = 0
	local switch_urban = 0
	local switch_pre08 = 0
	local switch_post08 = 0
	local switch_post00 = 1
	
local switch_controls = 0 //adds controls for free college (dummies) and for the unemployment rate
local switch_acs_controls = 1 //post00 NEEDS to be on when this switch is on!!!


********************************************************************************
**CREATE STACKED DATA**
********************************************************************************

if `switch_rerun_stacking'{
	do "${main}/code/build_stacked_data.do"
}

********************************************************************************
**ENROLLMENT DATA**
********************************************************************************
use "${main}/data/outcomes_enrollment.dta", clear


local type ""

if `switch_nponly'{
	drop if control == 3
	local type  "_nponly`type'"
}
if `switch_fponly'{
	keep if control == 3
	local type  "_fponly`type'"
}
if `switch_ftonly'{
	keep if efalevel == 22
	local type  "_FT`type'"
}
if `switch_ptonly'{
	keep if efalevel == 42
	local type  "_PT`type'"
}
if `switch_pubonly'{
	keep if control == 1
	local type  "_public`type'"
}
if `switch_prionly'{
	keep if control == 2
	local type  "_private`type'"
}
if `switch_menonly'{
	replace eftotlt = eftotlm
	local type  "_men`type'"
}
if `switch_womenonly'{
	replace eftotlt = eftotlw
	local type  "_women`type'"
}
if `switch_msu'{
	keep if msu
	local type  "_msu`type'"
}
if `switch_urban'{
	keep if urban
	local type  "_urban`type'"
}
if `switch_pre08'{
	keep if file_year < 2008
	local type  "_pre08`type'"
}
if `switch_post08'{
	keep if file_year > 2008
	local type  "_post08`type'"
}
if `switch_post00'{
	keep if file_year >= 2000
	local type  "_post0017`type'"
}
else if `switch_all'{
	local type = "_all`type'"
}

//collapse by state
collapse (sum) eftotlt , by(file_year deg st_fips)
	rename eftotlt eftotl
	drop if st_fips > 56 //this drops all of the US territories
	drop if file_year < 1986 // this drops 1980, 1984 and 1985, for which our data are much spottier
	
if `switch_controls'{
	//generate indicators for if a state has a free CC program in a given year
	gen free_cc = 0
		replace free_cc = 1 if st_fips == 5  & file_year > 2016
		replace free_cc = 1 if st_fips == 6  & file_year > 2018
		replace free_cc = 1 if st_fips == 9  & file_year > 2019
		replace free_cc = 1 if st_fips == 10 & file_year > 2005
		replace free_cc = 1 if st_fips == 13 & file_year > 2013
		replace free_cc = 1 if st_fips == 15 & file_year > 2017
		replace free_cc = 1 if st_fips == 18 & file_year > 1990
		replace free_cc = 1 if st_fips == 19 & file_year > 2019
		replace free_cc = 1 if st_fips == 20 & file_year > 2016
		replace free_cc = 1 if st_fips == 21 & file_year > 2016
		replace free_cc = 1 if st_fips == 22 & file_year > 2016
		replace free_cc = 1 if st_fips == 24 & file_year > 2016
		replace free_cc = 1 if st_fips == 28 & file_year > 2016
		replace free_cc = 1 if st_fips == 29 & file_year > 2016
		replace free_cc = 1 if st_fips == 30 & file_year > 2016
		replace free_cc = 1 if st_fips == 32 & file_year > 2016
		replace free_cc = 1 if st_fips == 34 & file_year > 2016
		replace free_cc = 1 if st_fips == 36 & file_year > 2016
		replace free_cc = 1 if st_fips == 40 & file_year > 2016
		replace free_cc = 1 if st_fips == 41 & file_year > 2016
		replace free_cc = 1 if st_fips == 44 & file_year > 2016
		replace free_cc = 1 if st_fips == 46 & file_year > 2016
		replace free_cc = 1 if st_fips == 47 & file_year > 2016
		replace free_cc = 1 if st_fips == 53 & file_year > 2016
		replace free_cc = 1 if st_fips == 54 & file_year > 2016
		
	//add controls for unemployment rate by state
	preserve
		use "${main}/data/clean_controls_labormarket.dta", clear
		
		keep statefips unemp_rate year
			rename statefips st_fips
			rename year file_year
		
		tempfile unemployment
		save `unemployment'
	restore 
	
	merge m:1 file_year st_fips using `unemployment', assert(match using) keep(match) nogen 
		//need to assert match master so when we restrict years we can get rid of those not in our sample
		
	//add controls for log state population (18-30 years old)
	preserve
		use "${main}/data/census_yearly_statepop.dta", clear
			rename statefips st_fips
			rename year_academic file_year
		
		tempfile statepop
		save `statepop'
	restore 
	
	merge m:1 file_year st_fips using `statepop', assert(match using) keep(match) nogen 
		//need to assert match master so when we restrict years we can get rid of those not in our sample
	
	rename file_year year_academic
	rename st_fips statefips 
	keep year_academic statefips deg eftotl free_cc unemp_rate lnpop_1830
}
else if `switch_acs_controls'{
	//generate indicators for if a state has a free CC program in a given year
	gen free_cc = 0
		replace free_cc = 1 if st_fips == 5  & file_year > 2016
		replace free_cc = 1 if st_fips == 6  & file_year > 2018
		replace free_cc = 1 if st_fips == 9  & file_year > 2019
		replace free_cc = 1 if st_fips == 10 & file_year > 2005
		replace free_cc = 1 if st_fips == 13 & file_year > 2013
		replace free_cc = 1 if st_fips == 15 & file_year > 2017
		replace free_cc = 1 if st_fips == 18 & file_year > 1990
		replace free_cc = 1 if st_fips == 19 & file_year > 2019
		replace free_cc = 1 if st_fips == 20 & file_year > 2016
		replace free_cc = 1 if st_fips == 21 & file_year > 2016
		replace free_cc = 1 if st_fips == 22 & file_year > 2016
		replace free_cc = 1 if st_fips == 24 & file_year > 2016
		replace free_cc = 1 if st_fips == 28 & file_year > 2016
		replace free_cc = 1 if st_fips == 29 & file_year > 2016
		replace free_cc = 1 if st_fips == 30 & file_year > 2016
		replace free_cc = 1 if st_fips == 32 & file_year > 2016
		replace free_cc = 1 if st_fips == 34 & file_year > 2016
		replace free_cc = 1 if st_fips == 36 & file_year > 2016
		replace free_cc = 1 if st_fips == 40 & file_year > 2016
		replace free_cc = 1 if st_fips == 41 & file_year > 2016
		replace free_cc = 1 if st_fips == 44 & file_year > 2016
		replace free_cc = 1 if st_fips == 46 & file_year > 2016
		replace free_cc = 1 if st_fips == 47 & file_year > 2016
		replace free_cc = 1 if st_fips == 53 & file_year > 2016
		replace free_cc = 1 if st_fips == 54 & file_year > 2016
		
	//add controls for unemployment rate by state
	preserve
		use "${main}/data/clean_controls_labormarket.dta", clear
		
		keep statefips unemp_rate year
			rename statefips st_fips
			rename year file_year
		
		tempfile unemployment
		save `unemployment'
	restore 
	
	merge m:1 file_year st_fips using `unemployment', assert(match using) keep(match) nogen 
		//need to assert match master so when we restrict years we can get rid of those not in our sample
	
	//add controls for log state population (18-30 years old)
	preserve
		use "${main}/data/clean_acs_race_age_pop_state.dta", clear
			rename statefip st_fips
			rename year file_year
		
		tempfile statepop
		save `statepop'
	restore 
	
	merge m:1 file_year st_fips using `statepop', keep(match) nogen 
		//keep match drops the years outside of our sample (keeps only 2000-2020)

	rename file_year year_academic
	rename st_fips statefips 
	keep year_academic statefips deg eftotl free_cc unemp_rate lnpop_1830_total pct_*
}
//get acs population counts for standardizing enrollment numbers
else{	
	rename file_year year_academic
	rename st_fips statefips 
	keep year_academic statefips deg eftotl
}
		
reshape wide eftotl , i(year_academic statefips) j(deg)
	gen log_eftotl1 = ln(eftotl1)
	gen log_eftotl3 = ln(eftotl3)
			
tempfile enroll
save `enroll'

local var log_eftotl
local weights1  ""
local weights3  ""

local ytitle "log enrollment in college"


merge 1:m year_academic statefips using "${main}/data/mw_stacked.dta", keep(match) // keep matches drops years outside of our enrollment panel

********************************************************************************
**GRAPHING**
********************************************************************************

matrix col1 = J(8,2,.)
matrix col2 = J(8,3,.)
matrix results = J(8,5,.)

* define treatment vector
global treat F*treat L*treat

*linear combinations of post-reg coeffs
global lincomm3 F3treat - F1treat
global lincomm2 F2treat - F1treat
global lincomm1 F1treat - F1treat
global lincom0 L0treat - F1treat
global lincom1 L1treat - F1treat
global lincom2 L2treat - F1treat
global lincom3 L3treat - F1treat
global lincom4 L4treat - F1treat


preserve
	eststo twoyr:  reghdfe `var'1 $treat [`weights1']  , ///
			a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
			i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)
				local dof = e(df_r)
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col1[`i',1] = r(estimate)
						matrix col1[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col1[`i',1] = r(estimate)
						matrix col1[`i++',2] = r(se)
				}

	eststo fouryr:  reghdfe `var'3 $treat [`weights3']  , ///
			a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
			i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips) 
				local i = 1
				foreach year in 3 2 1{
					lincom (${lincomm`year'})
						matrix col2[`i',1] = r(estimate)
						matrix col2[`i++',2] = r(se)
				}
				foreach year in 0 1 2 3 4{
					lincom (${lincom`year'})
						matrix col2[`i',1] = r(estimate)
						matrix col2[`i++',2] = r(se)
				}
		matrix col2[1,3] = -3
		matrix col2[2,3] = -2
		matrix col2[3,3] = -1
		matrix col2[4,3] = 0
		matrix col2[5,3] = 1
		matrix col2[6,3] = 2
		matrix col2[7,3] = 3
		matrix col2[8,3] = 4

	matrix results = col1, col2

	clear
	matlist results
	svmat results

	rename results1  coeff_1
	rename results2  se_1
	rename results3  coeff_3
	rename results4  se_3
	rename results5  time

	foreach x in 1 3{
		gen ci_upper_`x' = coeff_`x' + invttail(`dof', 0.025)*se_`x'
		gen ci_lower_`x' = coeff_`x' - invttail(`dof', 0.025)*se_`x'
	}

	//save in tables raw 
	export excel ${output}/tables_raw.xlsx, sheet(MW`var'_`type'2) sheetreplace firstrow(var)
	
	
	//graphing style things
		grstyle init
		grstyle set plain
		grstyle set size 8pt: subheading axis_title
 
	twoway rarea ci_lower_1 ci_upper_1  time, color(maroon%45) ||  ///
			line coeff_1 time, lcolor(maroon) mcolor(maroon) || ///
			rarea ci_lower_3 ci_upper_3  time, color(emerald%30) ||  ///
			line coeff_3 time, lcolor(emerald) mcolor(emerald) lp(dash)  ///
			ylab(-.14(.04).06, grid labsize(small)) yscale(range(-.14 .06)) yline(0, lp(dash) lc(gs10)) ///
			xline(-1, lp(dash) lc(black)) xlab(-3(1)4, grid labsize(small)) legend(col(2) region(lstyle(none)) ///
			order(2 "Two-Year" 4 "Four-Year") size(small)) ytitle("`ytitle'") xtitle("years since minimum wage change")
		graph export "${output}/MW`var'_${dmwlevel}`type'.pdf", replace
		
restore


//version with controls
if `switch_controls'{
	preserve
		eststo twoyr_controls:  reghdfe `var'1 $treat free_cc unemp_rate lnpop_1830 , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)	
					local dof = e(df_r)
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}

		eststo fouryr_controls:  reghdfe `var'3 $treat free_cc unemp_rate lnpop_1830 , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips) 
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col2[`i',1] = r(estimate)
							matrix col2[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col2[`i',1] = r(estimate)
							matrix col2[`i++',2] = r(se)
					}
					matrix col2[1,3] = -3
					matrix col2[2,3] = -2
					matrix col2[3,3] = -1
					matrix col2[4,3] = 0
					matrix col2[5,3] = 1
					matrix col2[6,3] = 2
					matrix col2[7,3] = 3
					matrix col2[8,3] = 4

		matrix results = col1, col2

		clear
		matlist results
		svmat results

		rename results1  coeff_1
		rename results2  se_1
		rename results3  coeff_3
		rename results4  se_3
		rename results5  time

		foreach x in 1 3{
			gen ci_upper_`x' = coeff_`x' + invttail(`dof', 0.025)*se_`x'
			gen ci_lower_`x' = coeff_`x' - invttail(`dof', 0.025)*se_`x'
		}
		
		//save in tables raw 
		export excel ${output}/tables_raw.xlsx, sheet(MW`var'_`type'2c) sheetreplace firstrow(var)


		//graphing style things
			grstyle init
			grstyle set plain
			grstyle set size 8pt: subheading axis_title

		twoway rarea ci_upper_1 ci_lower_1 time, color(maroon%45) ||  ///
				line coeff_1 time, lcolor(maroon) mcolor(maroon) || ///
				rarea ci_upper_3 ci_lower_3 time, color(emerald%30) ||  ///
				line coeff_3 time, lcolor(emerald) mcolor(emerald) lp(dash)  ///
				ylab(-.14(.04).06, grid labsize(small)) yscale(range(-.14 .06)) ///
				yline(0, lp(dash) lc(gs10)) xline(-1, lp(dash) lc(black)) ///
				xlab(-3(1)4, grid labsize(small)) legend(col(2) order(2 "Two-Year" 4 "Four-Year") size(small)) ///
				ytitle("`ytitle'") xtitle("years since minimum wage change") legend(region(lstyle(none))) ///
				note(`"controls for log 18-30 year-old population, free CC, and unemployment rates"')
				
			graph export "${output}/MW`var'_${dmwlevel}`type'_controls.pdf", replace
			
	restore
}

if `switch_acs_controls'{
	preserve
		eststo twoyr_controls:  reghdfe `var'1 $treat free_cc unemp_rate lnpop_1830_total pct_* , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips)	
					local dof = e(df_r)
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col1[`i',1] = r(estimate)
							matrix col1[`i++',2] = r(se)
					}

		eststo fouryr_controls:  reghdfe `var'3 $treat free_cc unemp_rate lnpop_1830_total pct_* , ///
				a(i.event#i.statefips i.event#i.rel_time i.event#i.control_origeventpost ///
				i.event#control_fedeventpost i.event#control_othereventpost) cluster(dmw_statefips) 
					local i = 1
					foreach year in 3 2 1{
						lincom (${lincomm`year'})
							matrix col2[`i',1] = r(estimate)
							matrix col2[`i++',2] = r(se)
					}
					foreach year in 0 1 2 3 4{
						lincom (${lincom`year'})
							matrix col2[`i',1] = r(estimate)
							matrix col2[`i++',2] = r(se)
					}
					matrix col2[1,3] = -3
					matrix col2[2,3] = -2
					matrix col2[3,3] = -1
					matrix col2[4,3] = 0
					matrix col2[5,3] = 1
					matrix col2[6,3] = 2
					matrix col2[7,3] = 3
					matrix col2[8,3] = 4

		matrix results = col1, col2

		clear
		matlist results
		svmat results

		rename results1  coeff_1
		rename results2  se_1
		rename results3  coeff_3
		rename results4  se_3
		rename results5  time

		foreach x in 1 3{
			gen ci_upper_`x' = coeff_`x' + invttail(`dof', 0.025)*se_`x'
			gen ci_lower_`x' = coeff_`x' - invttail(`dof', 0.025)*se_`x'
		}


		//graphing style things
			grstyle init
			grstyle set plain
			grstyle set size 8pt: subheading axis_title

		twoway rarea ci_upper_1 ci_lower_1 time, color(maroon%45) ||  ///
				line coeff_1 time, lcolor(maroon) mcolor(maroon) || ///
				rarea ci_upper_3 ci_lower_3 time, color(emerald%30) ||  ///
				line coeff_3 time, lcolor(emerald) mcolor(emerald) lp(dash) ///
				ylab(-.14(.04).06, grid labsize(small)) yscale(range(-.14 .06)) ///
				yline(0, lp(dash) lc(gs10)) xline(-1, lp(dash) lc(black)) ///
				xlab(-3(1)4, grid labsize(small)) legend(col(2) order(2 "Two-Year" 4 "Four-Year") size(small)) ///
				ytitle("`ytitle'") xtitle("years since minimum wage change") legend(region(lstyle(none))) ///
				note(`"controls for racial breakdown of 18-30 year-old population, log 18-30 y/o pop, free CC,"' ///
				`"and unemployment rates (2000-2020 only)"')
				
			graph export "${output}/MW`var'_${dmwlevel}`type'_controls_acs.pdf", replace
			
	restore
}
